//
//  SlideBarCustomCell.m
//  WEIMO
//
//  Created by Lixiaoyong on 13-6-13.
//  Copyright (c) 2013年 广州从兴电子开发有限公司. All rights reserved.
//

#import "SlideBarCustomCell.h"

#import "KeyValueObject.h"

@interface SlideBarCustomCell ()
@property (nonatomic,retain) SlideBarItemButton *leftButton;
@property (nonatomic,retain) SlideBarItemButton *rightButton;
@end

@implementation SlideBarCustomCell
@synthesize leftButton,rightButton;
@synthesize delegate;

- (void)dealloc
{
    self.leftButton = nil;
    self.rightButton = nil;
    self.delegate = nil;
    [super dealloc];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundView = [[[UIView alloc] initWithFrame:self.frame] autorelease];
        self.backgroundView.backgroundColor = [UIColor clearColor];
        
        self.leftButton = [SlideBarItemButton buttonWithType:UIButtonTypeCustom];
        self.leftButton.frame = CGRectZero;
        [self.leftButton addTarget:self action:@selector(slideItemClickedEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.leftButton];
        
        self.rightButton = [SlideBarItemButton buttonWithType:UIButtonTypeCustom];
        self.rightButton.frame = CGRectZero;
        [self.rightButton addTarget:self action:@selector(slideItemClickedEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.rightButton];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
}

#pragma mark - protected methods

- (void)addDataToCell:(NSArray *)array withSize:(CGSize)size
{
    int count = array.count;
    if (count <= 0)
    {
        self.rightButton.hidden = YES;
        self.leftButton.hidden = YES;
        return;
    }
    
    float width = size.width / 2 - 1;
    CGRect rect = CGRectMake(0, 0, width, size.height);
    if (count >= 1)
    {
        KeyValueObject *item = [array objectAtIndex:0];
        self.leftButton.frame = rect;
        self.leftButton.tag = item.value.intValue;
        [self.leftButton refreshSelfFrame:rect];
        self.leftButton.itemName.text = item.keyDispName;
        self.leftButton.item = item;
        [self.leftButton refreshButtonState:NO];
        
        if (count == 2 && count <= 2)
        {
            rect.origin.x = rect.origin.x + rect.size.width + 1;
            KeyValueObject *twoItem = [array objectAtIndex:1];
            self.rightButton.frame = rect;
            self.rightButton.tag = twoItem.value.intValue;
            [self.rightButton refreshSelfFrame:rect];
            self.rightButton.itemName.text = twoItem.keyDispName;
            self.rightButton.item = twoItem;
            [self.rightButton refreshButtonState:NO];
        }
        else
        {
            self.rightButton.hidden = YES;
        }
    }
}

#pragma mark - event

- (void)slideItemClickedEvent:(SlideBarItemButton *)button
{
    if (self.delegate)
    {
        [self.delegate clickedSlideBarCustomCellItem:button];
    }
}

@end